﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using asMeishi.web.Models;
using publicLibrary;

using entityClass = asMeishi.web.Models.dict;
namespace asMeishi.web.Controllers
{
    
    public class dictController : Controller
    {

        DataSession<MongoDataContext, entityClass> db = new DataSession<MongoDataContext, entityClass>();

        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult Index()
        {

            ViewData.Model = db.query.ToList(); ;

            return View();
        }

        //
        // GET: /dict/Edit/5
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult Edit(string id)
        {


            ViewData.Model = db.GetById(id) ?? new entityClass();
            return View();
        }

        //
        // POST: /dict/Edit/5

        [HttpPost]
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {

                entityClass entity = db.GetById(id)??new entityClass() ;
                var oid = entity.Id;
                TryUpdateModel(entity);
                //entity.Id = oid;
                //if (entity.value == null) entity.value = new Dictionary<string, string>();
                db.Save(entity);

                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                return View();
            }
        }

        //
        // GET: /dict/Delete/5
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult Delete(string id)
        {

            ViewData.Model = db.GetById(id) ?? new entityClass();

            return View();
        }

        //
        // POST: /dict/Delete/5

        [HttpPost]
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {

                db.Delete(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult editItem(string id, string oldkey, string key, string value)
        {

            if (string.IsNullOrEmpty(key))
            {
                TempData["info"] = "键值不能为空！";
                return RedirectToAction("Edit", new { id = id });
            }

            try
            {

                entityClass entity = db.GetById(id);
                entity.value = entity.value ?? new Dictionary<string, string>();
                entity.value.Remove(oldkey);
                entity.value[key] = value;
                db.Save(entity);

                TempData["info"] = "成功！";
            }
            catch
            {
                TempData["info"] = "失败！";
            }

            return RedirectToAction("Edit", new { id = id });
        }
        [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
        public ActionResult delItem(string id, string key)
        {
            try
            {

                entityClass entity = db.GetById(id);
                entity.value.Remove(key);
                db.Save(entity);

                TempData["info"] = "成功！";
            }
            catch
            {
                TempData["info"] = "失败！";
            }

            return RedirectToAction("Edit", new { id = id });
        }

        #region AJAX
        [HttpGet]
        public ActionResult getValueByName(string name)
        {
            try
            {

                var xx = db.Get(d => d.name == name);
                var _jsonObj = MyJsonStr.getString(xx.value.ToArray());

                return Content(AjaxCallBackInfo.jsonOk(_jsonObj));
            }
            catch (Exception e)
            {
                return Content(AjaxCallBackInfo.jsonError(e.Message));
            }
        }

        #endregion

    }
}
